<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		computed: {
			...mapState(['openid', 'token'])
		},
		onLaunch: function() {
			let that = this;
			uni.getSetting({
				success(res) {
					console.log("授权：", res);
					if (!res.authSetting['scope.userInfo']) {
						//这里调用授权
						console.log("当前未授权");
						uni.reLaunch({ //信息更新成功后跳转到小程序首页
							url: '/pages/index/index'
						});
					} else {
						//用户已经授权过了
						console.log("当前已授权");
						that.getOpenid();
						
					}
				}
			})
		},
		methods: {
			...mapMutations(['setUser', 'setOpenid']),
			getOpenid: function() {
				let that = this;
				// 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log("登录", loginRes);
						var params = {
							url: 'users/getOpenid',
							type: 'POST',
							data: {
								code: loginRes.code
							},
							sCallback: function(res) {
								console.log('app login:', res);
								that.setOpenid(res);
								that.wxlogin(res);
							}
						}
						that.request(params);
						uni.hideLoading();
					},
				})
			},
			wxlogin: function(openid) {
				var that = this;
				var params = {
					url: 'users/wxlogin',
					type: 'POST',
					data: {
						openid: openid
					},
					sCallback: function(res) {
						console.log("wxlogin", res);
						if (res.token != "-1") {
							that.userInfo = res.user;
							that.userInfo["token"] = res.token;							
							that.setUser(that.userInfo);
						} else {
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									console.log('getUserInfo:', infoRes);
									that.userInfo = infoRes.userInfo;
									that.userInfo["id"] = 0;
									that.userInfo["username"] = openid;
									that.userInfo["token"] = res.token;	
									that.userInfo["wxNickName"] = "0";								
									that.setUser(that.userInfo);
								}
							});
						}
					}
				};
				that.request(params);
			},
			request: function(params) {
				let that = this;
				let url = that.globalData.URL + params.url;
				if (!params.type) {
					params.type = 'GET';
				}

				if (params.type == 'GET') {
					var headerType = 'application/json'
				} else {
					var headerType = 'application/x-www-form-urlencoded'
				}
				if (params.setUpUrl == false) {
					url = params.url;
				}
				console.log("request url:" + url);
				wx.request({
					url: url,
					data: params.data,
					method: params.type,
					header: {
						'content-type': headerType,
						Authorization: that.token
					},
					success: function(res) {
						//返回成功信息
						params.sCallback && params.sCallback(res.data);
					},
					fail: function(err) {
						console.log("发起请求失败...")
					}
				});
			}
		},
		globalData: {
			URL: "http://localhost:8000/api/",
			Link: "http://localhost:8000/",
		}
		// globalData: {
		// 	URL: "https://jxsbbx.gzmu.edu.cn/alumni/api/",
		// 	Link: "https://jxsbbx.gzmu.edu.cn/alumni/",
		// }
	}
</script>

<style>
	/*每个页面公共css */
</style>
